package org.myorg.kafka2rabbitmq.demo.rabbitmq;


import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import org.myorg.kafka2rabbitmq.demo.entitys.Student;


public class RabbitMQProducer {

	public final static String QUEUE_NAME = "student";
	public final static String host = "172.18.1.86";
	public final static int port = 6672;
	public final static String username = "guest";
	public final static String password = "guest";

	public static void main(String[] args) throws Exception {
		ConnectionFactory connectionFactory = new ConnectionFactory();
		connectionFactory.setHost(host);
		connectionFactory.setPort(port);
		connectionFactory.setUsername(username);
		connectionFactory.setPassword(password);

		Connection connection = connectionFactory.newConnection();
		Channel channel = connection.createChannel();
		channel.queueDeclare(QUEUE_NAME, true, false, false, null);

		// 我们这里演示发送一千条数据
		for (int i = 1; i < 100; i++) {
			Student student = new Student(i, "xiaoli-" + i, "passwd-" + i, i);
			channel.basicPublish("", QUEUE_NAME, null, JSON.toJSONString(student).getBytes("UTF-8"));
			System.out.println("send to rabbitmq " + student.toString());
		}

		channel.close();
		connection.close();
	}
}
